﻿using System;
using System.Linq;
using System.Reflection;
/// <summary>
/// Summary description for Linq
/// </summary>
public class Linq
{
    public Linq()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    public static void MapClassReflection(object source, ref object destination)
    {
        Type sourceType = source.GetType();
        Type destinationType = destination.GetType();

        foreach (PropertyInfo sourceproperty in sourceType.GetProperties())
        {
            PropertyInfo destinationproperty = destinationType.GetProperty(sourceproperty.Name);

            if (destinationproperty != null)
            {
                destinationproperty.SetValue(destination, sourceproperty.GetValue(source, null), null);
            }
        }

    }
    public static bool CompareClassReflection(object source, ref object destination, string[] arrPropsToBeCompared)
    {
        Type sourceType = source.GetType();
        Type destinationType = destination.GetType();

        foreach (PropertyInfo sourceproperty in sourceType.GetProperties())
        {
            PropertyInfo destinationproperty = destinationType.GetProperty(sourceproperty.Name);
            object destinationValue = destinationproperty.GetValue(destination, null);
            object sourceValue = sourceproperty.GetValue(source, null);
            if (!(destinationValue == null && sourceValue == null))
            {
                if (arrPropsToBeCompared.Length > 0 && arrPropsToBeCompared.Contains(sourceproperty.Name))
                {
                    if (((destinationValue == null && sourceValue != null) || (destinationValue != null && sourceValue == null))
                        || destinationValue.ToString() != sourceValue.ToString())
                        //If (either of the properties is null 
                        //|| the values of the two match) Then return true                                     
                        return true;
                }
            }
        }
        return false;
    }
}
